/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.dict.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.market.base.dict.mapper.entity.DictType;
import net.siufung.mybatis.base.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * <p>
 * 字典类型 Mapper 接口
 * </p>
 *
 * @author 陈建峰
 * @since 2022-07-08 15:30:16
 */
@Mapper
public interface IDictTypeMapper extends BaseMapper<DictType> {

    /**
     * 验证字典类型名称不重复
     * @param verifyRepeatDto 重复实体对象
     * @return Boolean
     */
    default Boolean checkDictTypeNameNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto) {
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0L == this.selectCount(new LambdaQueryWrapper<DictType>()
                    .eq(DictType::getTypeName, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0L == this.selectCount(new LambdaQueryWrapper<DictType>()
                    .eq(DictType::getTypeName, verifyRepeatDto.getVerifyKeyValue())
                    .ne(DictType::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

    /**
     * 验证字典类型编码不重复
     * @param verifyRepeatDto 重复实体对象
     * @return Boolean
     */
    default Boolean checkDictTypeCodeNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto) {
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0L == this.selectCount(new LambdaQueryWrapper<DictType>()
                    .eq(DictType::getTypeCode, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0L == this.selectCount(new LambdaQueryWrapper<DictType>()
                    .eq(DictType::getTypeCode, verifyRepeatDto.getVerifyKeyValue())
                    .ne(DictType::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }


}
